<template>
  <div class="app-container">
    <el-form :model="searchForm" label-width="80px" @keyup.enter.native="search()">
      <el-row>
        <el-col class="datePicker">
          <el-form-item label="时间区间" size="small">
            <el-date-picker type="date" placeholder="选择开始日期" value-format="yyyy-MM-dd" v-model="searchForm.startTimeStr"
            ></el-date-picker>
            <span>-</span>
            <el-date-picker type="date" placeholder="选择结束日期" value-format="yyyy-MM-dd" v-model="searchForm.endTimeStr"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="searchBtn">
          <el-form-item size="small">
            <el-button type="primary" icon="el-icon-search" @click="search()">搜索</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div class="table-btn">
      <el-button type="primary" size="small" icon="el-icon-plus" @click="addTeam()">添加</el-button>
      <el-dialog :title="bankBalanceTitle" :visible.sync="bankBalanceVisible" width="500px">
        <el-form ref="teamRow" :rules="rules" :model="teamRow" label-width="100px">
          <el-form-item label="银行名称" size="small">
            <el-input type="text" v-model="teamRow.bankName"></el-input>
          </el-form-item>
          <el-form-item label="银行卡号" size="small">
            <el-input type="text" v-model="teamRow.bankCode"></el-input>
          </el-form-item>
          <el-form-item label="持有者名称" size="small">
            <el-input type="text" v-model="teamRow.holder"></el-input>
          </el-form-item>
          <el-form-item label="上期余额" size="small">
            <el-input type="number" v-model.number="teamRow.balanceBefore">
              <span slot="suffix">万元</span>
            </el-input>
          </el-form-item>
          <el-form-item label="账上余额" size="small">
            <el-input type="number" v-model.number="teamRow.balance">
              <span slot="suffix">万元</span>
            </el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="warning" size="small"  @click="submitForm('teamRow')" v-no-more-click>确定</el-button>
          <el-button size="small" @click="bankBalanceVisible = false">取消</el-button>
        </div>
      </el-dialog>
    </div>
    <my-table
      :data="tableData"
      :col-configs=" colConfigs"
      v-loading="listLoading">
      <el-table-column
        slot="opt"
        fixed="right"
        label="操作"
        width="150"
      >
        <template slot-scope="scope">
          <el-button
            @click.native.prevent="details(scope.row)"
            type="text"
            size="small">
            明细
          </el-button>
          <el-button
            @click.native.prevent="register(scope.row)"
            type="text"
            size="small">
            登记
          </el-button>
          <el-button
            @click.native.prevent="editTeam(scope.row)"
            type="text"
            size="small">
            修改
          </el-button>
          <!--
          <el-popover
            placement="top"
            width="160"
            :ref="`popover-${scope.$index}`"
          >
            <p>确定删除这条数据吗?</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[`popover-${scope.$index}`].doClose()">取消</el-button>
              <el-button type="primary" size="mini" @click.native.prevent="deleteRow(scope)">确定</el-button>
            </div>
            <el-button type="text" size="small" slot="reference" v-popover:popover-${scope.$index}>删除</el-button>
          </el-popover>
          -->
        </template>
      </el-table-column>
    </my-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageNum"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    <el-dialog :title="dialogTitle" :visible.sync="outerVisible" width="700px" @close="closeDialog">
      <el-form label-width="110px">
        <el-form-item label="账目类型" size="small">
          <el-select v-model="accountType" placeholder="请选择">
            <el-option
              v-for="item in accountTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <component :is="accountType" keep-alive :outer-visible="outerVisible" :balance-id="balanceId"
                 @closeDialog="closeDialog" @fetchData="fetchData"></component>
    </el-dialog>
    <el-dialog title="详情" :visible.sync="detailsVisible" width="1100px">
      <component is="detail" :view-detail-list="viewDetailList" keep-alive></component>
    </el-dialog>
  </div>
</template>

<script>
  import {
    getList,
    insertBankBalance,
    selectBankBalance,
    updateBankBalance,
    delBankBalance
  } from '@/api/bankbalance'
  import myTable from '@/components/myTable'
  import global from '@/components/global'

  import costproject from '@/views/financial/accounts/components/costproject'
  import invoice from '@/views/financial/accounts/components/invoice'
  import projectcontrac from '@/views/financial/accounts/components/projectcontrac'
  import tenderdeposit from '@/views/financial/accounts/components/tenderdeposit'
  import projectdeposit from '@/views/financial/accounts/components/projectdeposit'
  import tendercompensation from '@/views/financial/accounts/components/tendercompensation'
  import currentaccount from '@/views/financial/accounts/components/currentaccount'
  import otherfees from '@/views/financial/accounts/components/otherfees'
  import detail from './components/details'

  import { AreaSelect } from 'vue-area-linkage'
  import { formatDate } from '@/utils/index'

  export default {
    components: {
      myTable,
      AreaSelect,
      costproject,
      invoice,
      projectcontrac,
      tenderdeposit,
      projectdeposit,
      tendercompensation,
      currentaccount,
      otherfees,
      detail
    },
    data() {
      return {
        colConfigs: [
          { prop: 'bankName', label: '银行名称', width: '150' },
          { prop: 'bankCode', label: '银行卡号', width: '200' },
          { prop: 'holder', label: '持有者名称', width: '150' },
          { prop: 'balanceBefore', label: '上期余额(万元)', minWidth: '25%' },
          { prop: 'balance', label: '账上余额(万元)', minWidth: '25%' },
          { prop: 'costSum', label: '支出总额(万元)', minWidth: '25%' },
          { prop: 'billSum', label: '到账总额(万元)', minWidth: '25%' },
          { slot: 'opt' }
        ],
        searchForm: {},
        searchFormList: {},
        professions: [],
        typeNames: [],
        listLoading: true,
        dialogType: 0,
        teamRow: {},
        teamRowClear: {},
        tableData: [],
        pageNum: 1,
        pageSize: 10,
        total: 1,
        rules: {},
        bankBalanceVisible: false,
        bankBalanceTitle: '',
        dialogTitle: '',
        balanceId: 0,
        outerVisible: false,
        accountType: 'costproject',
        accountTypeList: [
          { value: 'costproject', label: '成本支出' },
          // { value: 'invoice', label: '开票管理' },
          { value: 'projectcontrac', label: '项目到账' },
          { value: 'tenderdeposit', label: '投标保证金管理' },
          { value: 'projectdeposit', label: '履约保证金管理' },
          { value: 'tendercompensation', label: '投标补偿金到账' },
          { value: 'currentaccount', label: '往来账' },
          { value: 'otherfees', label: '其他到账' }
        ],
        detailsVisible: false, // 详情
        viewDetailList: {}
      }
    },
    created() {
      this.teamRowClear = JSON.parse(JSON.stringify(this.teamRow))
      this.fetchData()
    },
    computed: {
      global() {
        return global
      },
      roleType() {
        return this.$store.state.user.roleType
      },
      isDirector() {
        return this.$store.state.user.isDirector
      },
      teamId() {
        return this.$store.state.user.teamId
      }
    },
    methods: {
      fetchData() {
        this.searchFormList.pageNum = this.pageNum
        this.searchFormList.pageSize = this.pageSize
        this.listLoading = true
        getList(this.searchFormList).then(response => {
          this.tableData = response.responseBody.list
          this.total = response.responseBody.total
          this.listLoading = false
        })
      },
      search() {
        this.pageNum = 1
        this.searchFormList = JSON.parse(JSON.stringify(this.searchForm))
        this.fetchData()
      },
      addTeam() {
        if (this.$refs['teamRow']) {
          this.$refs['teamRow'].resetFields()
        }
        this.bankBalanceVisible = true
        this.bankBalanceTitle = '新增银行账号'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 1
        this.$set(this.teamRow, 'teamId', this.teamId)
      },
      editTeam(row) {
        this.bankBalanceVisible = true
        this.bankBalanceTitle = '修改银行账号'
        this.teamRow = JSON.parse(JSON.stringify(this.teamRowClear))
        this.dialogType = 0
        const params = {
          balanceId: row.balanceId
        }
        selectBankBalance(params).then(response => {
          this.teamRow = Object.assign({}, response.responseBody)
        })
      },
      register(row) {
        this.outerVisible = true
        this.dialogTitle = '登记'
        this.balanceId = row.balanceId
      },
      submitForm(formName) {
        const params = {
          teamId: this.teamRow.teamId,
          bankName: this.teamRow.bankName || '',
          bankCode: this.teamRow.bankCode || '',
          holder: this.teamRow.holder || '',
          balanceBefore: this.teamRow.balanceBefore || 0,
          balance: this.teamRow.balance || 0
        }
        if (!this.dialogType) {
          params.balanceId = this.teamRow.balanceId
        }
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if (this.dialogType) {
              insertBankBalance(params).then(response => {
                this.bankBalanceVisible = false
                this.fetchData()
              })
            } else {
              updateBankBalance(params).then(response => {
                this.bankBalanceVisible = false
                this.fetchData()
              })
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
      },
      deleteRow(scope) {
        console.log(this.$refs[`popover-${scope.$index}`])
        this.$refs[`popover-${scope.$index}`].doClose()
        const params = {
          balanceId: scope.row.balanceId,
          isDel: 1
        }
        delBankBalance(params).then(response => {
          this.fetchData()
        })
      },
      handleSizeChange(val) {
        this.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.pageNum = val
        this.fetchData()
      },
      closeDialog() {
        this.outerVisible = false
        this.fetchData()
      },
      details(row) {
        this.detailsVisible = true
        this.viewDetailList = { ...this.searchFormList, balanceId: row.balanceId }
      }
    },
    mounted() {
      console.log(this.pca)
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .app-container {
    .el-pagination {
      margin-top: 20px;
    }
    .table-btn {
      margin-bottom: 10px;
      & > button {
        margin: 0 20px;
      }
      .el-dialog {
        textarea {
          margin-top: 10px;
        }
        .el-checkbox {
          margin-right: 20px;
        }
        .el-checkbox + .el-checkbox {
          margin-left: 0;
        }
      }
    }
  }

</style>


<style rel="stylesheet/scss" lang="scss">
  .el-input__icon {
    line-height: 100%;
  }

  .el-form-item__content {
    line-height: 33px;
  }

  .area-select .area-selected-trigger {
    padding: 0 10px;
  }

  /*.el-table__body tr.hover-row>td {
    background-color: #0000ff !important;
  }

  .el-table__body tr.current-row>td {
    background-color: #0000ff !important;
  }*/
</style>
